package cn.kgc.filter;


import cn.kgc.pojo.Users;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //判断用户是否登录 --> 判断session中是否有用户
        HttpServletRequest rq = (HttpServletRequest)request;
        HttpServletResponse rp = (HttpServletResponse)response;
        Users users = (Users)rq.getSession().getAttribute("users");
        if (null == users){
            rp.sendRedirect("/news/jsp/error.jsp");
        }else {
            chain.doFilter(rq, rp);
        }
    }

    public void destroy() {
    }
}
